package net.hserver.admin.monitor.controller;

import cn.hserver.core.queue.HServerQueue;
import cn.hserver.plugin.web.annotation.Controller;
import cn.hserver.plugin.web.annotation.GET;
import cn.hserver.plugin.web.annotation.POST;
import cn.hserver.plugin.web.context.PartFile;
import cn.hserver.plugin.web.interfaces.HttpRequest;
import cn.hserver.plugin.web.interfaces.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import net.hserver.admin.monitor.entity.TableFile;
import net.hserver.admin.monitor.util.FileUtil;
import net.hserver.admin.system.common.MyResult;

import java.io.File;

@Slf4j
@Controller
public class FileController {


    @GET("/file")
    public void file(HttpRequest request, HttpResponse response) throws Exception {
        String query = request.query("file");
        response.setDownloadBigFile(new File(query));
    }


    @POST("/fileApi")
    public MyResult fileApi(HttpRequest request, HttpResponse response) {
        PartFile file = request.queryFile("file");
        try {
            String s = FileUtil.copyFileWithUUID(file.getFilePath(), file.pointExtName());
            String keyword = request.query("keyword");
            String content = request.query("content");
            String fileType = request.query("fileType");
            String filePath = request.query("filePath");
            String fileSize = request.query("fileSize");

            TableFile tableFile = new TableFile();
            tableFile.setFilePath(filePath);
            tableFile.setFileType(fileType);
            tableFile.setKeyword(keyword);
            tableFile.setContent(content);
            tableFile.setFilePath(filePath);
            tableFile.setContent(content);
            tableFile.setFileSize(Long.parseLong(fileSize));
            tableFile.setServerFilePath(s);
            HServerQueue.sendQueue("CHECK", tableFile);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            file.deleteTempCacheFile();
        }
        return MyResult.success();
    }
}
